package com.example.myapplication.ui.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.staggeredgrid.LazyVerticalStaggeredGrid
import androidx.compose.foundation.lazy.staggeredgrid.StaggeredGridCells
import androidx.compose.foundation.lazy.staggeredgrid.itemsIndexed
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.PullToRefreshBox
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import coil.compose.SubcomposeAsyncImage
import com.example.myapplication.R
import com.example.myapplication.network.api.Product
import com.example.myapplication.network.core.NetworkResult
import com.example.myapplication.ui.viewmodel.ProductViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HomeScreen(navController: NavController,viewModel: ProductViewModel = viewModel()) {
    val productStatue = viewModel.producsState.collectAsState()
    var isRefreshing by remember { mutableStateOf(false) }
    val rememberCoroutineScope = rememberCoroutineScope()

    val onRefresh:()-> Unit = {
        isRefreshing = true
        rememberCoroutineScope.launch {
            delay(2000)
            viewModel.getProducts() //刷新
            isRefreshing=false
        }
    }

    when (val state = productStatue.value) {
        is NetworkResult.Loading -> {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                Text("数据加载中...")
            }
        }

        is NetworkResult.Error -> {
            Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) {
                Text("数据加载失败...")
            }
        }

        is NetworkResult.Success<List<Product>> -> {

            PullToRefreshBox(
                isRefreshing = isRefreshing,
                onRefresh = onRefresh
            ) {
                LazyVerticalStaggeredGrid(
                    columns = StaggeredGridCells.Fixed(2),
                    contentPadding = PaddingValues(10.dp),
                    modifier = Modifier.fillMaxSize(),
                    verticalItemSpacing = 8.dp,
                    horizontalArrangement = Arrangement.spacedBy(8.dp)
                ) {
                    itemsIndexed(state.data) { index, product ->
                        println("单个商品：" + product)
                        Column(
                            modifier = Modifier.fillMaxWidth().clickable(onClick = {
                                navController.navigate("productDetail")
                            }),
                            horizontalAlignment = Alignment.Start,
                            verticalArrangement = Arrangement.Top


                        ) {
                            SubcomposeAsyncImage(
                                model = product.coverImage,
                                contentDescription = "封面图",
                                modifier = Modifier.fillMaxWidth(),
                                contentScale = ContentScale.Crop,
                                loading = {
                                    Image(
                                        painter = painterResource(R.drawable.default_image),
                                        contentDescription = "图片加载中..."
                                    )
                                }
                            )
                            Text(product.title)
                            Text(product.subTitle)
                            Text(
                                text = product.displayPrice,
                                fontSize = 30.sp,
                                color = Color.Red
                            )
                            Row(
                                modifier = Modifier.fillMaxWidth(),
                                horizontalArrangement = Arrangement.SpaceBetween
                            ) {
                                Text(
                                    text = "自营",
                                    color = Color.White,
                                    modifier = Modifier
                                        .padding(start = 5.dp, end = 5.dp)
                                        .background(color = Color.Red)
                                )

                                Text("${product.commentCount}条评论")

                                Text(
                                    text = "看相似",
                                    modifier = Modifier.background(color = Color(0xF2F2F2))
                                )
                            }
                        }
                    }
                }
            }
        }
    }
}